package com.common.frame.utils;

import android.util.Base64;

import com.common.frame.net.RetrofitManager;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import io.reactivex.Single;
import okhttp3.Request;
import okhttp3.Response;

/**
 * DownUtils
 * create by sunxy on 2020/11/4
 **/
public class DownUtils {

    /**
     * downBase64File
     */
    public static Single<String> downBase64File(String value, String fileType){
        return downBytesFile(Base64.decode(value, Base64.DEFAULT), fileType);
    }

    /**
     * downBytesFile
     */
    public static Single<String> downBytesFile(byte[] value, String savePath){
        return Single.just(savePath)
                .map(path -> {
                    saveFile(value, FileUtils.createFile(path));
                    return path;
                });
    }


    /**
     * downUrlFile
     */
    public static Single<String> downUrlFile(String url, String savePath){
        return Single.just(savePath)
                .map(path -> {
                    saveFile(url2InputStream(url), FileUtils.createFile(path));
                    return path;
                });
    }


    /**
     * url2Bytes
     */
    public static byte[] url2Bytes(String url) throws Exception {
        Request request = new Request.Builder().url(url).build();
        Response response = RetrofitManager.Companion.getManager().getOkHttpClick().newCall(request).execute();
        return response.body().bytes();
    }

    /**
     * url2Bytes
     */
    public static InputStream url2InputStream(String url) throws Exception {
        Request request = new Request.Builder().url(url).build();
        Response response = RetrofitManager.Companion.getManager().getOkHttpClick().newCall(request).execute();
        return response.body().byteStream();
    }


    /**
     * saveFile
     * @param bytes     bytes
     * @param saveFile 保存文件
     */
    public static boolean saveFile(byte[] bytes, File saveFile) throws Exception {
        FileOutputStream fos = null;
        DataOutputStream dos = null;
        try {
            fos = new FileOutputStream(saveFile);
            dos = new DataOutputStream(fos);
            dos.write(bytes);
            dos.flush();
            fos.flush();
        }finally {
            if (fos != null) fos.close();
            if (dos != null) dos.close();
        }
        return true;
    }

    /**
     * saveFile
     * @param inputStream     inputStream
     * @param saveFile 保存文件
     */
    public static boolean saveFile(InputStream inputStream, File saveFile) throws Exception {
        FileOutputStream fos = null;
        try {
            int index;
            byte[] bytes = new byte[1024];

            fos = new FileOutputStream(saveFile);
            while ((index = inputStream.read(bytes)) != -1) {
                fos.write(bytes, 0, index);
            }
            fos.flush();
        }finally {
            if (fos != null) fos.close();
            if (inputStream != null) inputStream.close();
        }
        return true;
    }

}
